package com.ruoyi.record.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.service.IBasicUserService;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.domain.RecordSociety;
import com.ruoyi.record.service.IRecordSocietyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 申请记录Controller
 * 
 * @author ruoyi
 * @date 2023-11-23
 */
@Controller
@RequestMapping("/record/society")
public class RecordSocietyController extends BaseController
{
    private String prefix = "record/society";

    @Autowired
    private IRecordSocietyService recordSocietyService;

    @Autowired
    private IBasicUserService basicUserService;

    @RequiresPermissions("record:society:view")
    @GetMapping()
    public String society(ModelMap mmap)
    {
        mmap.put("isAdmin", ShiroUtils.getSysUser().isAdmin());
        return prefix + "/society";
    }

    /**
     * 查询申请记录列表
     */
    @RequiresPermissions("record:society:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordSociety recordSociety)
    {
        startPage();
        List<RecordSociety> list = recordSocietyService.selectRecordSocietyList(recordSociety);
        return getDataTable(list);
    }

    /**
     * 导出申请记录
     */
    @Log(title = "申请记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecordSociety recordSociety)
    {
        List<RecordSociety> list = recordSocietyService.selectRecordSocietyList(recordSociety);
        ExcelUtil<RecordSociety> util = new ExcelUtil<RecordSociety>(RecordSociety.class);
        return util.exportExcel(list, "申请记录");
    }

    /**
     * 新增申请记录
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<RecordSociety> societyList = recordSocietyService.selectRecordSocietyList(ShiroUtils.getLoginName());
        mmap.put("societyList", societyList);
        return prefix + "/add";
    }

    /**
     * 新增保存申请记录
     */
    @RequiresPermissions("record:society:add")
    @Log(title = "申请记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordSociety recordSociety)
    {
        recordSociety.setDeptId(ShiroUtils.getDeptId());
        recordSociety.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recordSocietyService.insertRecordSociety(recordSociety));
    }

    /**
     * 修改申请记录
     */
    @GetMapping("/edit/{societyId}")
    public String edit(@PathVariable("societyId") Long societyId, ModelMap mmap)
    {
        RecordSociety recordSociety = recordSocietyService.selectRecordSocietyBySocietyId(societyId);
        mmap.put("recordSociety", recordSociety);
        return prefix + "/edit";
    }

    /**
     * 修改保存申请记录
     */
    @RequiresPermissions("record:society:edit")
    @Log(title = "申请记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordSociety recordSociety)
    {
        recordSociety.setDeptId(ShiroUtils.getDeptId());
        recordSociety.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recordSocietyService.updateRecordSociety(recordSociety));
    }

    /**
     * 删除申请记录
     */
    @RequiresPermissions("record:society:remove")
    @Log(title = "申请记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordSocietyService.deleteRecordSocietyBySocietyIds(ids));
    }

    /**
     * 提交请假审核
     */
    @GetMapping("/submit/{societyId}")
    public String submit(@PathVariable("societyId") Long societyId, ModelMap mmap)
    {
        RecordSociety recordSociety = recordSocietyService.selectRecordSocietyBySocietyId(societyId);
        mmap.put("recordSociety", recordSociety);
        return prefix + "/submit";
    }

    /**
     * 请假记录提交
     */
    @RequiresPermissions("record:society:submit")
    @Log(title = "申请记录", businessType = BusinessType.OTHER)
    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submitSave(RecordSociety recordSociety)
    {
        recordSociety.setVerifyFlag("1");
        return toAjax(recordSocietyService.verifyRecordSociety(recordSociety));
    }

    /**
     * 事项审批打印
     */
    @GetMapping("/print/{societyId}")
    public String print(@PathVariable("societyId") Long societyId, ModelMap mmap)
    {
        RecordSociety recordSociety = recordSocietyService.selectRecordSocietyByPrint(societyId);
        mmap.put("recordSociety", recordSociety);
        if(recordSociety.getUserType().equals("01")) {
            return prefix + "/print";
        }else {
            return prefix + "/print1";
        }
    }

    @RequiresPermissions("record:verify:view")
    @GetMapping("/verify/{verifyType}")
    public String verify(@PathVariable("verifyType") String verifyType, ModelMap mmap)
    {
        BasicUser basicUser = basicUserService.selectBasicUserByIdCard(ShiroUtils.getLoginName());
        if(StringUtils.isNotNull(basicUser) && !ShiroUtils.getSubject().hasRole("administrator")) {
            mmap.put(verifyType + "Id", basicUser.getId());
        }
        mmap.put("verifyType", verifyType);
        return prefix + "/verify/society";
    }

    /**
     * 查询请假记录列表
     */
    @RequiresPermissions("record:verify:list")
    @PostMapping("/verify/list")
    @ResponseBody
    public TableDataInfo verifyList(RecordSociety recordSociety)
    {
        startPage();
        List<RecordSociety> list = recordSocietyService.selectVerifySocietyList(recordSociety);
        return getDataTable(list);
    }

    /**
     * 提交请假审核
     */
    @GetMapping("/verify/verify/{societyId}/{verifyType}")
    public String verify(@PathVariable("societyId") Long societyId, @PathVariable("verifyType") String verifyType, ModelMap mmap)
    {
        RecordSociety recordSociety = recordSocietyService.selectRecordSocietyBySocietyId(societyId);
        mmap.put("recordSociety", recordSociety);
        mmap.put("verifyType", verifyType);
        return prefix + "/verify/verify";
    }

    /**
     * 请假记录审批
     */
    @RequiresPermissions("record:verify:save")
    @Log(title = "申请记录", businessType = BusinessType.OTHER)
    @PostMapping("/verify/save")
    @ResponseBody
    public AjaxResult verifySave(RecordSociety recordSociety)
    {
        recordSociety.setVerifyFlag("2");
        return toAjax(recordSocietyService.updateRecordSociety(recordSociety));
    }
}
